package org.tong.activiti6.test.commons;

import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.deploy.DeploymentManager;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.activiti.engine.impl.util.ProcessInstanceHelper;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;

/**
 * @author
 * @date 2022-08-08 19:57
 */
public class StartAtFlowNodeCommand implements Command<ProcessInstance> {

    private String procDefinitionId;

    private String flowNodeId;

    private Long businessId;

    /**
     * 返回创建的实例
     */
    private ProcessInstance processInstance;


    public StartAtFlowNodeCommand(String procDefinitionId, String flowNodeId, Long businessId) {
        this.procDefinitionId = procDefinitionId;
        this.flowNodeId = flowNodeId;
        this.businessId = businessId;
    }

    @Override
    public ProcessInstance execute(CommandContext commandContext) {
        DeploymentManager deploymentManager = commandContext.getProcessEngineConfiguration().getDeploymentManager();
        // 查询流程定义
        ProcessDefinition processDefinition = deploymentManager.findDeployedProcessDefinitionById(procDefinitionId);
        if (ObjectUtils.isEmpty(processDefinition)) {
            throw new ActivitiObjectNotFoundException("No process definition found for id = " + procDefinitionId, ProcessDefinition.class);
        }
        Process process = ProcessDefinitionUtil.getProcess(procDefinitionId);
        FlowElement flowNode = process.getFlowElements().stream().filter(flowElement -> flowElement.getId().equals(flowNodeId) && flowElement instanceof UserTask)
                .findFirst().orElseThrow(() -> new RuntimeException("未找到节点"));

        ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
        //创建流程实例，但是不启动流程
        processInstance = processInstanceHelper.createProcessInstance((ProcessDefinitionEntity) processDefinition, String.valueOf(businessId), null, null, new HashMap<>());
        //获取第一个流程实例，设置当前节点为当前flowNodeId
        ExecutionEntity executionEntity = ((ExecutionEntity) processInstance).getExecutions().get(0);
        executionEntity.setCurrentFlowElement(flowNode);
        commandContext.getAgenda().planContinueProcessOperation(executionEntity);
        return null;
    }

    public ProcessInstance getProcessInstance() {
        return processInstance;
    }
}
